import axios from 'axios'
import router from '@/router'
import {Notification, MessageBox,Message} from 'element-ui'
import store from '../store'
import {getToken} from '@/utils/tokenUtil'
import Config from '@/config'
import {da} from "pinyin/data/dict-zi-web";

// 创建axios实例
const service = axios.create({
    baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
    // withCredentials: true, // 跨越请求时发送cookies
    timeout: Config.timeout, // 请求超时
    headers: {
        'Content-Type': 'application/json; charset=utf-8'
    }
})


service.interceptors.request.use(
    config => {
        let token=getToken();
        if(token)
            config.headers[Config.jwtHeader] = 'Bearer ' + token;

        return config
    },
    error => { // 请求错误处理
        console.log(error)

        return Promise.reject(error)
    }
)

// 响应拦截器
service.interceptors.response.use(
    response => {
        const status = response.status
        if(status===200){
            const data=response.data
            let code=data.code||0
            if(code===0){
                return data.data
            }else if(code===401){
                MessageBox.confirm(
                    '登录状态已过期，请重新登录!',
                    '系统提示',
                    {
                        confirmButtonText: '重新登录',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }
                ).then(() => {
                    store.dispatch('user/logout').finally(res=>{
                        location.reload()
                    })
                })
            }else if(code===403){
                router.push({path: '/403'})
            }else{
                Notification({
                    title: data.message||'网络或服务器错误!',
                    type: 'error',
                    duration: 2500
                })
            }
            return Promise.reject()
        }else{
            Notification({
                message: response.data.message||'网络或服务器错误!',
                type: 'error',
                duration: 2500
            })
            return Promise.reject()
        }
    },
    error => {
        Notification({
            message: error.toString()||'网络或服务器错误!',
            type: 'error',
            duration: 2500
        })
        return Promise.reject(error)
    }
)
export default service
